package top.stackstrace.sock;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetServer;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;

public class MainVerticle extends AbstractVerticle {


  @Override
  public void start(Promise<Void> startPromise) throws Exception {
    NetServer server = vertx.createNetServer();

    server.connectHandler(socket->{
      System.out.println("connect accepted.");
      socket.handler(new MsgWorker(socket));
      socket.exceptionHandler(e->{
        e.printStackTrace();
      }).closeHandler((e)->{
        System.out.println("socket closed");
      });
    });

    server.exceptionHandler(e->{
      e.printStackTrace();
    });

    server.listen(8080).onComplete(res -> {
      if (res.succeeded()) {
        startPromise.complete();
      } else {
        System.out.println("accept failed.");
        startPromise.fail("");
      }
    });
  }
}
